package com.jiuwan.gamepass.service.impl;

import com.jiuwan.gamepass.dao.AppDao;
import com.jiuwan.gamepass.dao.FeedbackDao;
import com.jiuwan.gamepass.dto.appJson.FeedBackJson;
import com.jiuwan.gamepass.pojo.App;
import com.jiuwan.gamepass.pojo.Feedback;
import com.jiuwan.gamepass.service.FeedBackService;
import com.jiuwan.gamepass.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @version 1.0
 * 功能目的描述 ：用于         ========>>>>>>>>>>>
 * @author： pang-yun
 * @date： 2021-06-10 09:52
 */

@Service
public class FeedBackServiceImpl implements FeedBackService {

    @Autowired
    private AppDao appDao;
    @Autowired
    private FeedBackJson feedBackJson;
    @Autowired
    private Feedback feedback;
    @Autowired
    private FeedbackDao feedbackDao;

    @Override
    public FeedBackJson uploadWord(String english, String translate, String appID) {

        //检测 传入的app ID 所代表的app是否存在
        App app = appDao.selectByPrimaryKey(appID);
        if (app == null) {
            feedBackJson.setCode(0);
            feedBackJson.setMsg("输入关联的appID有误");
            return feedBackJson;
        }

        //传入 feedback
        String uuid = UUIDUtil.createUUID();
        feedback.setId(uuid);
        feedback.setAppID(appID);
        feedback.setEnglish(english);
        feedback.setTranslate(translate);
        Date date = new Date();
        feedback.setUpdateTime(date);

        int insert = feedbackDao.insert(feedback);
        if (insert == 0) {
            feedBackJson.setCode(0);
            feedBackJson.setMsg("存入数据库失败");
            return feedBackJson;
        }

        feedBackJson.setCode(1);
        feedBackJson.setMsg("success");
        feedBackJson.setFeedback(feedback);
        return feedBackJson;

    }

    @Override
    public FeedBackJson selectByAppID(String appID) {
        Feedback feedback = feedbackDao.selectByAppID(appID);
        if (feedback == null) {
            feedBackJson.setCode(0);
            feedBackJson.setMsg("输入的appID有误");
            return feedBackJson;
        }
        feedBackJson.setCode(1);
        feedBackJson.setMsg("success");
        feedBackJson.setFeedback(feedback);
        return feedBackJson;
    }

    @Override
    public FeedBackJson updateByAppID(String english, String translate, String appID) {
        FeedBackJson feedBackJson = new FeedBackJson();
        Feedback feedback = feedbackDao.selectByAppID(appID);
        if (feedback == null) {
            this.feedBackJson.setCode(0);
            this.feedBackJson.setMsg("输入的appID有误");
            return this.feedBackJson;
        }
        feedback.setEnglish(english);
        feedback.setTranslate(translate);
        int i = feedbackDao.updateByAppID(feedback);
        if (i == 0) {
            this.feedBackJson.setCode(0);
            this.feedBackJson.setMsg("更新失败");
            return this.feedBackJson;
        }
        this.feedBackJson.setCode(1);
        this.feedBackJson.setMsg("success");
        this.feedBackJson.setFeedback(feedback);
        return this.feedBackJson;

    }

    @Override
    public FeedBackJson deleteByAppID(String appID) {

        int i = feedbackDao.deleteByAppID(appID);
        if (i == 0) {
            feedBackJson.setCode(0);
            feedBackJson.setMsg("删除失败");
            return feedBackJson;
        }
        feedBackJson.setCode(1);
        feedBackJson.setMsg("success");
        return feedBackJson;
    }

}
